package com.wu.controller;

import com.wu.pojo.ArticleDraft;
import com.wu.response.CommonReturnType;
import com.wu.service.ArticleDraftService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

import static com.wu.constant.Constant.DEFAULT_NULL_STRING;
import static com.wu.constant.Constant.DEFAULT_NULL_VALUE;

/**
 * 文章草稿控制类
 *
 * @author: 吴磊
 * @program: blog
 * @create: 2021-05-09 14:01
 */
@RestController
@Api(tags = "文章草稿")
public class ArticleDraftController {

    @Autowired
    private ArticleDraftService articleDraftService;


    /**
     * 插入文章草稿
     *
     * @param articleContent 文章草稿内容
     * @param articleTitle   文章标题
     * @param articleId      文章ID，如果传了就说明是已有的草稿
     * @return 返回一个文章ID
     */
    @ApiOperation(value = "插入文章草稿")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "articleContent", value = "文章草稿内容", dataTypeClass = String.class),
            @ApiImplicitParam(name = "articleTitle", value = "文章标题", dataTypeClass = String.class),
            @ApiImplicitParam(name = "articleId", value = "文章ID，如果传了就说明是已有的草稿", dataTypeClass = String.class)})
    @RequestMapping(value = "/addArticleDraft", method = RequestMethod.POST)
    public CommonReturnType tokenAddArticle(
            @RequestParam("articleContent") String articleContent,
            @RequestParam("articleTitle") String articleTitle,
            @RequestParam(name = "articleId", required = false, defaultValue = DEFAULT_NULL_VALUE) String articleId
    ) {
        ArticleDraft articleDraft;
        if (StringUtils.isEmpty(articleId) || DEFAULT_NULL_STRING.equals(articleId)) {
            articleDraft = articleDraftService.insertArticleDraft(articleTitle, articleContent);
        } else {
            articleDraft = articleDraftService.updateArticleDraft(articleId, articleTitle, articleContent);
        }
        return CommonReturnType.create(articleDraft);
    }

    /**
     * 获取用户文章草稿
     *
     * @return
     */
    @ApiOperation(value = "获取用户文章草稿")
    @RequestMapping(value = "/getArticleDrafts", method = RequestMethod.GET)
    public CommonReturnType tokenGetArticleDraftsBuyUserId() {
        return CommonReturnType.create(articleDraftService.getArticleDraftListByUserId());
    }

    /**
     * 根据文章草稿ID删除文章草稿
     *
     * @param articleId
     * @return
     */
    @ApiOperation(value = "根据文章草稿ID删除文章草稿")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "articleId", value = "文章草稿ID", dataTypeClass = String.class)})
    @RequestMapping(value = "/deleteArticleDraftById", method = RequestMethod.POST)
    public CommonReturnType tokenDeleteArticleDraftById(@RequestParam("articleId") String articleId) {
        articleDraftService.deleteArticleDraftByArticleId(articleId);
        return CommonReturnType.create(null);
    }

    /**
     * 根据文章ID获取文章草稿
     *
     * @param articleId
     * @return
     */
    @ApiOperation(value = "根据文章ID获取文章草稿")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "articleId", value = "文章草稿ID", dataTypeClass = String.class)})
    @RequestMapping(value = "/getArticleDraftByArticleId", method = RequestMethod.POST)
    public CommonReturnType tokenGetArticleDraftByArticleId(@RequestParam("articleId") String articleId) {
        Map<String, Object> map = articleDraftService.getArticleDraftByArticleId(articleId);
        return CommonReturnType.create(map);
    }
}
